package com.meihua.baoleyi.ui;

import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.meihua.baoleyi.base.BaseActivity;


/**
 * Created by ${zhangyuanchao} on 2017/10/18.
 */

public abstract class BaseWebViewActivity extends BaseActivity {
    public WebView mWebView;

    public void initWebView(WebView mWebView, final ProgressBar mLoading, String url, final TextView tvtitle) {
        this.mWebView = mWebView;
        WebSettings settings = mWebView.getSettings();
        mWebView.loadUrl(url);
        settings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        settings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
        settings.setDefaultTextEncodingName("utf-8");
        settings.setBuiltInZoomControls(true);
        settings.setDisplayZoomControls(false); //隐藏原生的缩放控件
        settings.setUseWideViewPort(true);
        settings.setJavaScriptEnabled(true);

        // 设置允许JS弹窗
        settings.setJavaScriptCanOpenWindowsAutomatically(true);

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                tvtitle.setText(title);
            }

            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                Toast.makeText(BaseWebViewActivity.this,message,Toast.LENGTH_SHORT).show();
                result.confirm();
                return true;
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (null == mLoading) return;
                if (newProgress < 100) {
                    mLoading.setProgress(newProgress);
                } else {
                    mLoading.setVisibility(View.GONE);
                }
            }
        });

        //监听网页的加载操作
        mWebView.setWebViewClient(new WebViewClient() {


            //设置不用系统浏览器打开,直接显示在当前Webview
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (null == mWebView) return;
        mWebView.resumeTimers();
    }

    @Override
    protected void onPause() {
        super.onPause();
        /*暂停全局*/
        if (null == mWebView) return;
        mWebView.pauseTimers();
    }

    @Override
    protected void onDestroy() {
        if (null == mWebView) return;
        /*清除缓存 历史记录*/
        mWebView.clearCache(true);
        mWebView.clearHistory();
        /*销毁*/
        if (mWebView != null) {
            mWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            mWebView.clearHistory();

            ((ViewGroup) mWebView.getParent()).removeView(mWebView);
            mWebView.destroy();
            mWebView = null;
        }
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (null == mWebView) return false;
        if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
